package com.volunteer.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.volunteer.entity.Notification;
import com.volunteer.mapper.NotificationMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 通知公告服务
 */
@Service
@RequiredArgsConstructor
public class NotificationService {

    private final NotificationMapper notificationMapper;

    /**
     * 分页查询通知列表
     */
    public IPage<Notification> getNotificationList(int page, int size, String type) {
        Page<Notification> pageParam = new Page<>(page, size);
        LambdaQueryWrapper<Notification> queryWrapper = new LambdaQueryWrapper<>();
        
        queryWrapper.eq(Notification::getStatus, 1);  // 只查询已发布的
        
        if (type != null && !type.isEmpty()) {
            queryWrapper.eq(Notification::getType, type);
        }
        
        queryWrapper.orderByDesc(Notification::getCreateTime);
        return notificationMapper.selectPage(pageParam, queryWrapper);
    }

    /**
     * 获取通知详情
     */
    public Notification getNotificationById(Long id) {
        return notificationMapper.selectById(id);
    }

    /**
     * 创建通知
     */
    public Notification createNotification(Notification notification) {
        if (notification.getStatus() == null) {
            notification.setStatus(1);
        }
        notificationMapper.insert(notification);
        return notification;
    }

    /**
     * 更新通知
     */
    public void updateNotification(Notification notification) {
        notificationMapper.updateById(notification);
    }

    /**
     * 删除通知
     */
    public void deleteNotification(Long id) {
        notificationMapper.deleteById(id);
    }
}

